package com.zycfc.zsf.boot.util.text;

import java.util.regex.*;
import com.alibaba.fastjson.*;
import org.dom4j.*;
import java.util.*;
import com.zycfc.zsf.boot.util.json.*;
import org.slf4j.*;

public class TxtVarParseUtils
{
    private static final Logger log;
    
    public static Set<String> findVar(final String prefix, final String txt) {
        final Set<String> list = new HashSet<String>();
        final Pattern regex = Pattern.compile("\\" + prefix + "\\{([^}]*)\\}");
        final Matcher matcher = regex.matcher(txt);
        while (matcher.find()) {
            list.add(matcher.group(1));
        }
        return list;
    }
    
    public static String replaceVar(final String prefix, final Map<String, Object> parameter, String txt) {
        final Set<String> parameters = findVar(prefix, txt);
        for (final String p : parameters) {
            final Object o = parameter.get(p);
            txt = txt.replace(prefix + "{" + p + "}", String.valueOf(o));
        }
        return txt;
    }
    
    public static String replaceVar(final String prefix, final String json, String txt) {
        final Set<String> list = findVar(prefix, txt);
        for (final String key : list) {
            String value = getValue("json", key, json);
            value = (StringUtils.isEmpty(value) ? "" : value);
            txt = replaceVar(prefix, txt, key, value);
        }
        return txt;
    }
    
    public static String getValue(final String dataType, final String fieldName, String data) {
        if (StringUtils.isEmpty(fieldName) || StringUtils.isEmpty(dataType)) {
            return null;
        }
        String value = null;
        if ("xml".equals(dataType)) {
            final int position = data.indexOf("<?xml");
            if (position > 0) {
                data = data.substring(position);
            }
            value = getXmlValue(fieldName, data);
        }
        else if ("json".equals(dataType)) {
            value = getJsonValue(fieldName, data);
        }
        return value;
    }
    
    public static String getJsonValue(final String fieldName, String jsonData) {
        if (StringUtils.isEmpty(fieldName)) {
            return null;
        }
        final String[] split;
        final String[] fields = split = fieldName.split("\\.");
        for (final String f : split) {
            final JSONObject jsonObject = JSONObject.parseObject(jsonData);
            jsonData = jsonObject.getString(f);
            if (StringUtils.isEmpty(jsonData)) {
                break;
            }
        }
        return jsonData;
    }
    
    public static String getXmlValue(final String fieldName, final String xmlData) {
        if (StringUtils.isEmpty(fieldName)) {
            return null;
        }
        final String xPath = "//" + fieldName.replaceAll("\\.", "//");
        Document doc;
        try {
            doc = DocumentHelper.parseText(xmlData);
        }
        catch (DocumentException ex) {
            TxtVarParseUtils.log.debug(ex.getMessage(), (Throwable)ex);
            return null;
        }
        return doc.selectSingleNode(xPath).getText();
    }
    
    public static List<Element> getXmlSubElement(final String xPath, final String xml, final Map<String, String> params) {
        List<Element> result = new ArrayList<Element>();
        if (StringUtils.isEmpty(xPath)) {
            return result;
        }
        try {
            final Document doc = DocumentHelper.parseText(xml);
            final XPath xpath = doc.createXPath(xPath);
            xpath.setNamespaceURIs((Map)params);
            final Element node = (Element)xpath.selectSingleNode((Object)doc);
            result = (List<Element>)node.elements();
        }
        catch (DocumentException ex) {
            TxtVarParseUtils.log.error(ex.getMessage(), (Throwable)ex);
        }
        return result;
    }
    
    @Deprecated
    private static List<String> findVar2(final String prefix, final String txt) {
        final List<String> list = new ArrayList<String>();
        final Pattern regex = Pattern.compile(prefix + "\\{.*?\\}" + prefix);
        final Matcher matcher = regex.matcher(txt);
        while (matcher.find()) {
            list.add(matcher.group(0));
        }
        return list;
    }
    
    @Deprecated
    private static String replaceVar(final String prefix, String txt, final String key, final String val) {
        txt = txt.replace(prefix + "{" + key + "}", val);
        return txt;
    }
    
    @Deprecated
    private static String replaceAllVar(final String prefix, String txt, final String key, final String val) {
        txt = txt.replaceAll(prefix + "{" + key + "}", val);
        return txt;
    }
    
    public static void main(final String[] args) {
        final Map<String, Object> map = new HashMap<String, Object>();
        map.put("apiCd", "WXD056");
        map.put("version", "1.0.0");
        final Map<String, Object> data = new HashMap<String, Object>();
        data.put("applicant_mobile", "112");
        map.put("data", data);
        final String json = JsonUtils.objectToJson(map);
        final Map<String, Object> req = JsonUtils.jsonToMap(json);
        final String txt = "if ('${version}'=='V1.0' && '${apiCd}'=='ZYXF056'){ '${data.applicant_mobile}'=='111' } elsif ('${version}'=='2.0.0' && '${apiCd}'=='ZYXF056'){ '${data.applicantMobile}'=='111' } elsif ('${version}'=='1.0.0' && '${apiCd}'=='WXD056'){ '${data.applicant_mobile}'=='111' }";
        final Set<String> list = findVar("$", txt);
        System.out.println(list);
        final String a = replaceVar("$", json, txt);
        System.out.println(a);
    }
    
    static {
        log = LoggerFactory.getLogger((Class)TxtVarParseUtils.class);
    }
}
